package edu.unf.cen6940.service;

import edu.unf.cen6940.dao.CategoryDAO;
import edu.unf.cen6940.dao.FacultyDAO;
import edu.unf.cen6940.dao.FacultyresearchDAO;
import edu.unf.cen6940.dao.FacultyresearchtostudentDAO;
import edu.unf.cen6940.dao.ResearchkeywordDAO;

import edu.unf.cen6940.domain.Category;
import edu.unf.cen6940.domain.Faculty;
import edu.unf.cen6940.domain.Facultyresearch;
import edu.unf.cen6940.domain.Facultyresearchtostudent;
import edu.unf.cen6940.domain.Researchkeyword;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Facultyresearch entities
 * 
 */

@Service("FacultyresearchService")
@Transactional
public class FacultyresearchServiceImpl implements FacultyresearchService {

	/**
	 * DAO injected by Spring that manages Category entities
	 * 
	 */
	@Autowired
	private CategoryDAO categoryDAO;

	/**
	 * DAO injected by Spring that manages Faculty entities
	 * 
	 */
	@Autowired
	private FacultyDAO facultyDAO;

	/**
	 * DAO injected by Spring that manages Facultyresearch entities
	 * 
	 */
	@Autowired
	private FacultyresearchDAO facultyresearchDAO;

	/**
	 * DAO injected by Spring that manages Facultyresearchtostudent entities
	 * 
	 */
	@Autowired
	private FacultyresearchtostudentDAO facultyresearchtostudentDAO;

	/**
	 * DAO injected by Spring that manages Researchkeyword entities
	 * 
	 */
	@Autowired
	private ResearchkeywordDAO researchkeywordDAO;

	/**
	 * Instantiates a new FacultyresearchServiceImpl.
	 *
	 */
	public FacultyresearchServiceImpl() {
	}

	/**
	 * Delete an existing Faculty entity
	 * 
	 */
	@Transactional
	public Facultyresearch deleteFacultyresearchFaculty(Integer facultyresearch_facultyResearchId, Integer related_faculty_facultyId) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyresearch_facultyResearchId, -1, -1);
		Faculty related_faculty = facultyDAO.findFacultyByPrimaryKey(related_faculty_facultyId, -1, -1);

		facultyresearch.setFaculty(null);
		related_faculty.getFacultyresearchs().remove(facultyresearch);
		facultyresearch = facultyresearchDAO.store(facultyresearch);
		facultyresearchDAO.flush();

		related_faculty = facultyDAO.store(related_faculty);
		facultyDAO.flush();

		facultyDAO.remove(related_faculty);
		facultyDAO.flush();

		return facultyresearch;
	}

	/**
	 * Return a count of all Facultyresearch entity
	 * 
	 */
	@Transactional
	public Integer countFacultyresearchs() {
		return ((Long) facultyresearchDAO.createQuerySingleResult("select count(o) from Facultyresearch o").getSingleResult()).intValue();
	}

	/**
	 * Return all Facultyresearch entity
	 * 
	 */
	@Transactional
	public List<Facultyresearch> findAllFacultyresearchs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Facultyresearch>(facultyresearchDAO.findAllFacultyresearchs(startResult, maxRows));
	}

	/**
	 * Save an existing Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public Facultyresearch saveFacultyresearchFacultyresearchtostudents(Integer facultyResearchId, Facultyresearchtostudent related_facultyresearchtostudents) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyResearchId, -1, -1);
		Facultyresearchtostudent existingfacultyresearchtostudents = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(related_facultyresearchtostudents.getFacultyResearchId(), related_facultyresearchtostudents.getStudentId(), related_facultyresearchtostudents.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyresearchtostudents != null) {
			existingfacultyresearchtostudents.setFacultyResearchId(related_facultyresearchtostudents.getFacultyResearchId());
			existingfacultyresearchtostudents.setStudentId(related_facultyresearchtostudents.getStudentId());
			existingfacultyresearchtostudents.setFacultyId(related_facultyresearchtostudents.getFacultyId());
			related_facultyresearchtostudents = existingfacultyresearchtostudents;
		}

		related_facultyresearchtostudents.setFacultyresearch(facultyresearch);
		related_facultyresearchtostudents.setFacultyResearchId(facultyResearchId);
		facultyresearch.getFacultyresearchtostudents().add(related_facultyresearchtostudents);
		related_facultyresearchtostudents = facultyresearchtostudentDAO.store(related_facultyresearchtostudents);
		facultyresearchtostudentDAO.flush();

		facultyresearch = facultyresearchDAO.store(facultyresearch);
		facultyresearchDAO.flush();

		return facultyresearch;
	}

	/**
	 * Delete an existing Researchkeyword entity
	 * 
	 */
	@Transactional
	public Facultyresearch deleteFacultyresearchResearchkeywords(Integer facultyresearch_facultyResearchId, Integer related_researchkeywords_researchKeywordId) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyresearch_facultyResearchId, -1, -1);
		Researchkeyword related_researchkeywords = researchkeywordDAO.findResearchkeywordByPrimaryKey(related_researchkeywords_researchKeywordId, -1, -1);

		facultyresearch.getResearchkeywords().remove(related_researchkeywords);
		related_researchkeywords.getFacultyresearchs().remove(facultyresearch);
		facultyresearch = facultyresearchDAO.store(facultyresearch);
		facultyresearchDAO.flush();

		related_researchkeywords = researchkeywordDAO.store(related_researchkeywords);
		researchkeywordDAO.flush();

		researchkeywordDAO.remove(related_researchkeywords);
		researchkeywordDAO.flush();

		return facultyresearch;
	}

	/**
	 * Save an existing Category entity
	 * 
	 */
	@Transactional
	public Facultyresearch saveFacultyresearchCategories(Integer facultyResearchId, Category related_categories) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyResearchId, -1, -1);
		Category existingcategories = categoryDAO.findCategoryByPrimaryKey(related_categories.getCategoryId());

		// copy into the existing record to preserve existing relationships
		if (existingcategories != null) {
			existingcategories.setCategoryId(related_categories.getCategoryId());
			existingcategories.setCategoryDescription(related_categories.getCategoryDescription());
			related_categories = existingcategories;
		} else {
			related_categories = categoryDAO.store(related_categories);
			categoryDAO.flush();
		}

		facultyresearch.getCategories().add(related_categories);
		related_categories.getFacultyresearchs().add(facultyresearch);
		facultyresearch = facultyresearchDAO.store(facultyresearch);
		facultyresearchDAO.flush();

		related_categories = categoryDAO.store(related_categories);
		categoryDAO.flush();

		return facultyresearch;
	}

	/**
	 * Save an existing Researchkeyword entity
	 * 
	 */
	@Transactional
	public Facultyresearch saveFacultyresearchResearchkeywords(Integer facultyResearchId, Researchkeyword related_researchkeywords) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyResearchId, -1, -1);
		Researchkeyword existingresearchkeywords = researchkeywordDAO.findResearchkeywordByPrimaryKey(related_researchkeywords.getResearchKeywordId());

		// copy into the existing record to preserve existing relationships
		if (existingresearchkeywords != null) {
			existingresearchkeywords.setResearchKeywordId(related_researchkeywords.getResearchKeywordId());
			existingresearchkeywords.setKeyword(related_researchkeywords.getKeyword());
			related_researchkeywords = existingresearchkeywords;
		} else {
			related_researchkeywords = researchkeywordDAO.store(related_researchkeywords);
			researchkeywordDAO.flush();
		}

		facultyresearch.getResearchkeywords().add(related_researchkeywords);
		related_researchkeywords.getFacultyresearchs().add(facultyresearch);
		facultyresearch = facultyresearchDAO.store(facultyresearch);
		facultyresearchDAO.flush();

		related_researchkeywords = researchkeywordDAO.store(related_researchkeywords);
		researchkeywordDAO.flush();

		return facultyresearch;
	}

	/**
	 * Delete an existing Facultyresearch entity
	 * 
	 */
	@Transactional
	public void deleteFacultyresearch(Facultyresearch facultyresearch) {
		facultyresearchDAO.remove(facultyresearch);
		facultyresearchDAO.flush();
	}

	/**
	 * Load an existing Facultyresearch entity
	 * 
	 */
	@Transactional
	public Set<Facultyresearch> loadFacultyresearchs() {
		return facultyresearchDAO.findAllFacultyresearchs();
	}

	/**
	 * Save an existing Facultyresearch entity
	 * 
	 */
	@Transactional
	public void saveFacultyresearch(Facultyresearch facultyresearch) {
		Facultyresearch existingFacultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyresearch.getFacultyResearchId());

		if (existingFacultyresearch != null) {
			if (existingFacultyresearch != facultyresearch) {
				existingFacultyresearch.setFacultyResearchId(facultyresearch.getFacultyResearchId());
				existingFacultyresearch.setTitle(facultyresearch.getTitle());
				existingFacultyresearch.setResearchDescription(facultyresearch.getResearchDescription());
				existingFacultyresearch.setResearchStatus(facultyresearch.getResearchStatus());
				existingFacultyresearch.setMinumumRequirements(facultyresearch.getMinumumRequirements());
			}
			facultyresearch = facultyresearchDAO.store(existingFacultyresearch);
		} else {
			facultyresearch = facultyresearchDAO.store(facultyresearch);
		}
		facultyresearchDAO.flush();
	}

	/**
	 */
	@Transactional
	public Facultyresearch findFacultyresearchByPrimaryKey(Integer facultyResearchId) {
		return facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyResearchId);
	}

	/**
	 * Delete an existing Facultyresearchtostudent entity
	 * 
	 */
	@Transactional
	public Facultyresearch deleteFacultyresearchFacultyresearchtostudents(Integer facultyresearch_facultyResearchId, Integer related_facultyresearchtostudents_facultyResearchId, Integer related_facultyresearchtostudents_studentId, Integer related_facultyresearchtostudents_facultyId) {
		Facultyresearchtostudent related_facultyresearchtostudents = facultyresearchtostudentDAO.findFacultyresearchtostudentByPrimaryKey(related_facultyresearchtostudents_facultyResearchId, related_facultyresearchtostudents_studentId, related_facultyresearchtostudents_facultyId, -1, -1);

		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyresearch_facultyResearchId, -1, -1);

		related_facultyresearchtostudents.setFacultyresearch(null);
		facultyresearch.getFacultyresearchtostudents().remove(related_facultyresearchtostudents);

		facultyresearchtostudentDAO.remove(related_facultyresearchtostudents);
		facultyresearchtostudentDAO.flush();

		return facultyresearch;
	}

	/**
	 * Delete an existing Category entity
	 * 
	 */
	@Transactional
	public Facultyresearch deleteFacultyresearchCategories(Integer facultyresearch_facultyResearchId, Integer related_categories_categoryId) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyresearch_facultyResearchId, -1, -1);
		Category related_categories = categoryDAO.findCategoryByPrimaryKey(related_categories_categoryId, -1, -1);

		facultyresearch.getCategories().remove(related_categories);
		related_categories.getFacultyresearchs().remove(facultyresearch);
		facultyresearch = facultyresearchDAO.store(facultyresearch);
		facultyresearchDAO.flush();

		related_categories = categoryDAO.store(related_categories);
		categoryDAO.flush();

/*		categoryDAO.remove(related_categories);
		categoryDAO.flush();*/

		return facultyresearch;
	}

	/**
	 * Save an existing Faculty entity
	 * 
	 */
	@Transactional
	public Facultyresearch saveFacultyresearchFaculty(Integer facultyResearchId, Faculty related_faculty) {
		Facultyresearch facultyresearch = facultyresearchDAO.findFacultyresearchByPrimaryKey(facultyResearchId, -1, -1);
		Faculty existingfaculty = facultyDAO.findFacultyByPrimaryKey(related_faculty.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfaculty != null) {
			existingfaculty.setFacultyId(related_faculty.getFacultyId());
			existingfaculty.setNnumber(related_faculty.getNnumber());
			existingfaculty.setFirstName(related_faculty.getFirstName());
			existingfaculty.setLastName(related_faculty.getLastName());
			existingfaculty.setEmail(related_faculty.getEmail());
			existingfaculty.setUrl(related_faculty.getUrl());
			existingfaculty.setActive(related_faculty.getActive());
			related_faculty = existingfaculty;
		}

		facultyresearch.setFaculty(related_faculty);
		related_faculty.getFacultyresearchs().add(facultyresearch);
		facultyresearch = facultyresearchDAO.store(facultyresearch);
		facultyresearchDAO.flush();

		related_faculty = facultyDAO.store(related_faculty);
		facultyDAO.flush();

		return facultyresearch;
	}
}
